package net.zdreamer.geracomium.ui.more;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.geracomium.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import net.zdreamer.geracomium.adapter.IndentListAdapter;
import net.zdreamer.geracomium.entity.IndentListInfos;
import net.zdreamer.geracomium.ui.BaseActivity;

public class IndentListActiity extends BaseActivity implements AdapterView.OnItemClickListener {
    private PullToRefreshListView indentlist;
    private IndentListAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_indent_list_actiity;
    }

    @Override
    protected void init() {
        indentlist = (PullToRefreshListView) findViewById(R.id.indentlist);
        indentlist.setMode(PullToRefreshBase.Mode.BOTH);
        IndentListInfos msgnoti = new IndentListInfos();
        mAdapter = new IndentListAdapter(this, msgnoti.indentlistinfos,
                R.layout.indentlist_item);
        indentlist.setAdapter(mAdapter);
        indentlist.setOnRefreshListener(orl);
        ListView actualListView = indentlist.getRefreshableView();
    }

    @Override
    protected void initViews() {
        initTitle("膳食订单列表", R.drawable.selector_title_back, 0, true, false);
    }

    @Override
    protected void initListener() {

    }

    private PullToRefreshBase.OnRefreshListener<ListView> orl = new PullToRefreshBase.OnRefreshListener<ListView>() {

        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
            String label = DateUtils.formatDateTime(getApplicationContext(),
                    System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                            | DateUtils.FORMAT_SHOW_DATE
                            | DateUtils.FORMAT_ABBREV_ALL);

            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(
                    "上次刷新：" + label);

            Thread a = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Message message = new Message();
                    message.what = 0;
                    h.sendMessage(message);
                }
            });
            a.start();
        }
    };
    Handler h = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                IndentListInfos msgnoti = new IndentListInfos();
                mAdapter = new IndentListAdapter(IndentListActiity.this, msgnoti.indentlistinfos,
                        R.layout.indentlist_item);
                indentlist.setAdapter(mAdapter);
                indentlist.onRefreshComplete();
            }
        }

    };


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(this, "tiaozhuan", Toast.LENGTH_LONG).show();
    }
}
